/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2020-12-28 09:52:56
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-01-11 16:58:34
 */
export function fontsize(size) {
  let deviceWidth = document.documentElement.clientWidth || window.innerWidth
  if (deviceWidth >= 1920 || deviceWidth == undefined) {
    deviceWidth = 1920
  }
  return (deviceWidth / 1920) * size
}
export function GetXCount(x0, min, x1) {
  var count = ((parseInt(x1.split(':')[0]) - parseInt(x0.split(':')[0])) * 60 + parseInt(x1.split(':')[1]) - parseInt(x0.split(':')[1])) / min - 1
  return count
}
export function GetXData(x0, min, count) {
  var xData = new Array()
  xData[0] = x0
  for (var i = 1; i < count; i++) {
    var stime = xData[xData.length - 1]
    var hour = stime.split(':')[0]
    var minute = stime.split(':')[1]

    var nexthour = parseInt(hour)
    var nextminute = parseInt(minute) + min
    if (nextminute == 60) {
      nextminute = 0
      nexthour = parseInt(hour) + 1
    }
    if (nexthour < 10) {
      hour = '0' + nexthour
      //hour = nexthour;
    } else {
      hour = nexthour
    }
    if (nextminute < 10) {
      minute = '0' + nextminute
    } else {
      minute = nextminute
    }
    xData[xData.length] = hour + ':' + minute
  }
  return xData
}
export function GetXtime() {
  var mytime24 = new Array()
  var i = 0
  var now_data = new Date()
  var year_now = now_data.getFullYear(),
    month_now = now_data.getMonth() + 1,
    day_now = now_data.getDate()
  var time_pre = `${year_now}/${month_now}/${day_now} 00:00:00`
  var ttt = new Date(time_pre).getTime()
  for (; i < 25; i++) {
    mytime24.push([ttt, '']) //传入value的值为空则该值点不会显示在图表中
    ttt = ttt + 3600000
  }
  return mytime24
}
/**
 * 排序
 * 根据一个字段排序
 * @param
 */
export function sortCommon(dataList, name) {
  var data = dataList
  for (let i = 0; i < data.length - 1; i += 1) {
    //通过 this.length 次把第一位放到最后,完成排序
    //-i是因为最后的位置是会动态改变的，当完成一次后,最后一位会变成倒数第二位
    for (let j = 0; j < data.length - 1 - i; j += 1) {
      if (data[j][name] < data[j + 1][name]) {
        const temp = data[j]
        data[j] = data[j + 1]
        data[j + 1] = temp
      }
    }
  }
  return data
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成 2018/09/23 11:54:16
 * @param datetime 国际化日期格式
 */
export function format(datetime) {
  return formatWithSeperator(datetime, '/', ':')
}
export function formatX(datetime) {
  return formatWithSeperator(datetime, '-', ':')
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018/09/23 11:54:16
 * 可以指定日期和时间分隔符
 * @param datetime 国际化日期格式
 */
export function formatWithSeperator(datetime, dateSeprator, timeSeprator) {
  if (datetime != null) {
    const dateMat = new Date(datetime)
    const year = dateMat.getFullYear()
    const month = dateMat.getMonth() + 1
    const day = dateMat.getDate()
    const hh = dateMat.getHours()
    const mm = dateMat.getMinutes()
    const ss = dateMat.getSeconds()
    const timeFormat =
      year + dateSeprator + (month < 10 ? '0' + month : month) + dateSeprator + (day < 10 ? '0' + day : day) + ' ' + hh + timeSeprator + mm + timeSeprator + ss
    return timeFormat
  }
}
export function newFormData(info) {
  const form = new FormData()
  for (const key in info) {
    form.append(key, info[key])
  }
  return form
}

// 获取当前日期时间
export function getDatetime() {
  var now = new Date()
  var year = now.getFullYear()
  var month = now.getMonth() + 1
  var day = now.getDate()
  var hh = now.getHours()
  var mm = now.getMinutes()
  var ss = now.getSeconds()
  var clock = year + '-'
  if (month < 10) clock += '0'
  clock += month + '-'
  if (day < 10) clock += '0'
  clock += day + ' '
  if (hh < 10) clock += '0'
  clock += hh + ':'
  if (mm < 10) clock += '0'
  clock += mm + ':'
  if (ss < 10) clock += '0'
  clock += ss
  return clock
}
// 获取当前日期--只获取到天
export function getDatetimeToDay() {
  var now = new Date()
  var year = now.getFullYear()
  var month = now.getMonth() + 1
  var day = now.getDate()
  var hh = now.getHours()
  var mm = now.getMinutes()
  var ss = now.getSeconds()
  var clock = year + '-'
  if (month < 10) clock += '0'
  clock += month + '-'
  if (day < 10) clock += '0'
  clock += day + ' '
  return clock
}
//获取近七天日期
export function getSevenDay() {
  var dateList = []
  for (var i = 0; i < 7; i++) {
    var date = new Date()
    date.setDate(date.getDate() - i)
    // console.log(
    //   date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
    // );
    var a = date.getMonth() + 1 + '-' + date.getDate()
    dateList.push(a)
  }
  dateList = dateList.reverse()
  return dateList
}
//********************************************************************************** */

// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time.replace(new RegExp(/-/gm), '/')
    }
    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}
// 回显数据字典
export function selectDictLabel(datas, value) {
  var actions = []
  Object.keys(datas).some((key) => {
    if (datas[key].dictValue == '' + value) {
      actions.push(datas[key].dictLabel)
      return true
    }
  })
  return actions.join('')
}

// 回显数据字典（字符串数组）
export function selectDictLabels(datas, value, separator) {
  var actions = []
  var currentSeparator = undefined === separator ? ',' : separator
  var temp = value.split(currentSeparator)
  Object.keys(value.split(currentSeparator)).some((val) => {
    Object.keys(datas).some((key) => {
      if (datas[key].dictValue == '' + temp[val]) {
        actions.push(datas[key].dictLabel + currentSeparator)
      }
    })
  })
  return actions.join('').substring(0, actions.join('').length - 1)
}
/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data, id, parentId, children) {
  let config = {
    id: id || 'id',
    parentId: parentId || 'parentId',
    childrenList: children || 'children'
  }

  var childrenListMap = {}
  var nodeIds = {}
  var tree = []

  for (let d of data) {
    let parentId = d[config.parentId]
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = []
    }
    nodeIds[d[config.id]] = d
    childrenListMap[parentId].push(d)
  }

  for (let d of data) {
    let parentId = d[config.parentId]
    if (nodeIds[parentId] == null) {
      tree.push(d)
    }
  }

  for (let t of tree) {
    adaptToChildrenList(t)
  }

  function adaptToChildrenList(o) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]]
    }
    if (o[config.childrenList]) {
      for (let c of o[config.childrenList]) {
        adaptToChildrenList(c)
      }
    }
  }
  return tree
}
